﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using ArgusPCLib;
using ArgusPCLib.Bird;

namespace Imagenator
{
	static class ServerInfo
	{
		static Task<Uri[]> _task;
		public static Uri MailServerAdress { get { return ServerInfo._task.Result[0]; } }
		public static Uri UpdateInfoAdress { get { return ServerInfo._task.Result[1]; } }

		static readonly Uri ResourceServerAdress = new Uri("http://argusmagnus.wordpress.com/app-infos/");
		const string MailServerId = "MailServerAdress";
		const string UpdateInfoId = "UpdateInfoImagenator";

		static ServerInfo()
		{
			ServerInfo._task = ResourceInfoServer.GetUrisAsync(ServerInfo.ResourceServerAdress, ServerInfo.MailServerId, ServerInfo.UpdateInfoId).ContinueWith<Uri[]>(new Func<Task<Uri[]>, Uri[]>((task) =>
			{
				if (task.Result == null)
					return new Uri[2];
				using (HttpClient client = new HttpClient())
				{
					Uri[] uris = task.Result;
					try
					{
						string s = client.GetStringAsync(uris[0]).Result;
						uris[0] = new Uri(s);
					}
					catch
					{
						uris[0] = null;
					}
					return uris;
				}
			}));
		}

		public static async Task InitializeAsync()
		{
			await ServerInfo._task;
		}
	}
}
